package 二叉树相关问题;

public class 从根到叶的二进制之和 {
    int sum=0;
    public int sumRootToLeaf(TreeNode root) {
        sumRoot(root,0);
        return sum;
    }

    private void sumRoot(TreeNode root, int num) {
        if (root==null)
            return;
        if (root.left==null && root.right==null)
            sum+=num*2+root.val;
        sumRoot(root.left,num*2+root.val);
        sumRoot(root.right,num*2+root.val);
    }

}
